﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using HMIS.MVC.ViewModel;
using HMIS.MVC.CommonSupport;
using HMIS.FrameWork;
using HMIS.Infrastructure.Core.Authority;
using HMIS.Infrastructure.Facade.Authority;
using HMIS.FrameWork.Cache;
using System.ComponentModel;

namespace HMIS.MVC.Controllers
{
    public class InfraDepartmentController : BaseController
    {
        private readonly IDepartmentFacade DepartmentFacade;
        public InfraDepartmentController(IDepartmentFacade DepartmentFacade)
        {
            this.DepartmentFacade = DepartmentFacade;
        }

        [Description("访问机构管理页面")]
        [ViewPage]
        public ActionResult Department()
        {
            return View();
        }

        [ExtResult]
        [Description("删除机构")]
        public ActionResult Delete()
        {
            DepartmentFacade.Delete(Request["Id"]);
            return this.JsonFormat((new ExtResult { success = true }));
        }


        [Description("获取机构列表")]
        public ActionResult GetPlist()
        {
            long total;
            var data = DepartmentFacade.GetPlist(
				int.Parse(Request["start"] == null ? "0" : Request["start"]),
				int.Parse(Request["limit"] == null ? "0" : Request["limit"]),
                Request["sort"],
                Request["dir"],
                this.DesDataFilter(Request["filter"]),
                out total);
            return this.JsonFormat(
                new ExtGirdData<Department> { total = total, data = data },
                new string[] { "CustomType" });
        }

        [ExtResult]
        [Description("判断机构的名称、编码是否在类别下唯一")]
        public ActionResult IsFieldExist()
        {
            var result = DepartmentFacade.IsFieldExist(Request["FieldName"], Request[Request["FieldName"]], Request["Id"]);
            return this.JsonFormat(new ExtResult { success = !result, msg = "已存在相同的项!" });

        }
        [ExtResult]
        [Description("添加、修改机构")]
        public ActionResult SaveOrUpdate()
        {
            Department entity;
            if (String.IsNullOrEmpty(Request["Id"]))
            {
                entity = new Department
                {
                    Name = Request["Name"],
                    Code = Request["Code"],
                    InputCode1 = Request["InputCode1"],
                    InputCode2 = Request["InputCode2"],
                    InputCode3 = Request["InputCode3"],
                    Description = Request["Description"]
                };
            }
            else
            {
                entity = DepartmentFacade.Get(Request["Id"]);
                entity.InputCode1 = Request["InputCode1"];
                entity.InputCode2 = Request["InputCode2"];
                entity.InputCode3 = Request["InputCode3"];
                entity.Description = Request["Description"];
            }

            DepartmentFacade.SaveOrUpdate(entity);
            return this.JsonFormat(new ExtResult { success = true, id = entity.Id });
        }
    }
}
